package com.bw.merchant.page.fragment

import android.app.Activity.RESULT_OK
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import com.blankj.utilcode.util.ToastUtils
import com.bw.common.view.BaseMvvmFragment
import com.bw.merchant.R
import com.bw.merchant.adapter.ImgAdapter
import com.bw.merchant.databinding.FragmentSettledBinding
import com.bw.merchant.entity.AddApplicationEntity
import com.bw.merchant.entity.AddApplicationEntityItem
import com.bw.merchant.vm.MerchantViewModel
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import kotlin.random.Random

/**
 * 入驻申请
 */
@AndroidEntryPoint
class SettledFragment : BaseMvvmFragment<FragmentSettledBinding>() {
    override fun getLayoutId(): Int = R.layout.fragment_settled
    private val mViewModel:MerchantViewModel by activityViewModels()

    var appNumber = 0
    var adapter = ImgAdapter()


    override fun initView() {

        //随机数
        mDataBinding.rvImg.adapter = adapter
        mDataBinding.addImg.setOnClickListener {
            if (adapter.data.size<4){
                PictureSelector.create(this,200).selectPicture()
            }else{
                ToastUtils.showLong("最多添加4张图片")
            }
            mDataBinding.rvImg.visibility = View.VISIBLE
        }

        mDataBinding.submit.setOnClickListener {

            appNumber = Random.nextInt(9000000)+1000000
            val addApplicationEntity = AddApplicationEntity(
                applicationArea = mDataBinding.addEnterpriseApplicationArea.text.toString().toInt(),
                applicationNumber = "RZ${appNumber}",
                contactMethod = 19233163384,
                contactName = "张三",
                documents = listOf(),
                enterpriseName = mDataBinding.addEnterpriseName.text.toString(),
                enterpriseProfileId = 189,
                entryPersonnel = mDataBinding.addEnterprisePeopleCount.text.toString().toInt(),
                entryTime = SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()),
                id = 0,
                processingSteps = listOf(AddApplicationEntityItem("1",1,"1")),
                status = "新建"
            )
            mViewModel.getAddMerchantData(addApplicationEntity)
            Log.d("--->sjs","${appNumber}")
        }


    }

    override fun loadData() {
        super.loadData()
        mViewModel.mAddMerchantSuccess.observe(this){
            ToastUtils.showLong("入驻成功")
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==200&&resultCode == RESULT_OK){
            val bean = data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            adapter.addData(bean?.path!!)
            adapter.notifyDataSetChanged()
        }
    }

}